package root.electronclass.electronclass.school

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import root.electronclass.electronclass.MainActivity
import root.electronclass.electronclass.R
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.databinding.FragmentClassBinding
import root.electronclass.electronclass.databinding.FragmentClassItemBinding
import root.electronclass.electronclass.ext.hideAll
import root.electronclass.electronclass.ext.show

class SchoolFragment : ViewBindingFragment<FragmentClassBinding>(), MainActivity.IPage {

    private val items = linkedMapOf<String, Class<out Fragment>>(
        "校园信息" to SchoolInfoFragment::class.java,
        "校园通知" to SchoolNotificationFragment::class.java,
        "校园宣传" to SchoolPropagandaFragment::class.java,
        "学习空间" to SchoolStudyFragment::class.java,
//        "教育动态" to SchoolDynamicFragment::class.java,
//        "校史馆" to SchoolHistoryFragment::class.java,
        "超级意见箱" to SchoolComplaintsBoxFragment::class.java,
    )
    private val icons = mapOf(
        "校园信息" to (R.drawable.ic_school_info to R.drawable.ic_school_info_text),
        "校园通知" to (R.drawable.ic_school_notification to R.drawable.ic_school_notification_text),
        "校园宣传" to (R.drawable.ic_school_propaganda to R.drawable.ic_school_propaganda_text),
        "学习空间" to (R.drawable.ic_school_study to R.drawable.ic_school_study_text),
//        "教育动态" to (R.drawable.ic_school_dynamic to R.drawable.ic_school_dynamic_text),
//        "校史馆" to (R.drawable.ic_school_history to R.drawable.ic_school_history_text),
        "超级意见箱" to (R.drawable.ic_school_complaints_box to R.drawable.ic_school_complaints_box_text)
    )

    private var selectItem: String = items.keys.first()
    private var isInit = false

    private val adapter = BasicAdapter<String, FragmentClassItemBinding> { holder, item ->
        val icon = icons[item] ?: return@BasicAdapter
        holder.binding.itemIcon.setImageResource(icon.first)
        holder.binding.itemText.text = item
        val isSelected = item == selectItem
        holder.itemView.isSelected = isSelected
        holder.binding.itemText.layoutParams.width = if (isSelected) 150 else 120

        holder.itemView.setOnClickListener {
            changeTo(item)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        adapter.refresh(items.keys)
        binding.recycler.adapter = adapter
        isInit = true
        changeTo(selectItem)
    }

    private fun changeTo(item: String?) {
        val lastPosition = items.keys.indexOfFirst { selectItem == it }
        val adapterPosition = items.keys.indexOfFirst { item == it }
        selectItem = adapter.list[adapterPosition]
        adapter.notifyItemChanged(lastPosition)
        adapter.notifyItemChanged(adapterPosition)
        val clazz = items[item] ?: return
        childFragmentManager.show(R.id.class_container, clazz, childFragmentManager.hideAll())
    }

    override fun onPageSelected(bundle: Bundle?) {
        //选中当前页面
        val clazz: Class<*> = bundle?.getSerializable("TARGET_SCHOOL") as? Class<*> ?: SchoolInfoFragment::class.java
        val index = items.values.toList().indexOf(clazz)
        if (isInit) {
            changeTo(items.keys.toList()[index])
        } else {
            selectItem = items.keys.toList()[index]
        }
    }
}